//
//  AttentionViewController.m
//  YIJU2.0HD
//
//  Created by 张金国 on 16/5/24.
//  Copyright © 2016年 Mr.X. All rights reserved.
//

#import "AttentionViewController.h"
#import "Masonry.h"
#import "AttentionCollectionViewCell.h"

#define kCellVecteSpecing 20//每个item之间的垂直间距
#define kCellHorisenSpecing 20//每个item之间的水平间距
#define kCellBackgroundColor [UIColor clearColor]//cell的背景色


@interface AttentionViewController ()<UICollectionViewDataSource,UICollectionViewDelegate,UICollectionViewDelegateFlowLayout>

@property (nonatomic ,strong)UICollectionView  *collectionView;

@property (nonatomic ,strong)UILabel       *label;

@property (nonatomic ,strong)NSMutableArray    *models;

@end

static NSString * CellIdentifier = @"GradientCell";


@implementation AttentionViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor whiteColor];
    CGFloat maxH = MIN(480,120);
    CGFloat minW = MIN(480, 400);
    self.preferredContentSize = CGSizeMake(minW, maxH);
    [self layoutUI];
}

- (void)layoutUI {
    
    [self.label mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.left.right.equalTo(self.view);
        make.height.equalTo(70);
    }];
    
    [self.collectionView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.label.bottom);
        make.left.right.equalTo(self.view);
        make.bottom.equalTo(self.view.mas_bottom);
    }];

}

#pragma mark -- UICollectionViewDataSource 

//定义展示的Section的个数
-(NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView
{
    return 1;
}

//每个UICollectionView展示的内容
-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
        AttentionCollectionViewCell * cell = [collectionView dequeueReusableCellWithReuseIdentifier:CellIdentifier forIndexPath:indexPath];
            cell.labelText = self.models[[indexPath row]];
    return cell;
}

//定义展示的UICollectionViewCell的个数
-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return self.models.count;
}

#pragma mark --UICollectionViewDelegateFlowLayout
//定义每个UICollectionView 的大小
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath
{
    return CGSizeMake(80, 100);
}

//定义每个UICollectionView 的 margin
-(UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout insetForSectionAtIndex:(NSInteger)section
{
    return UIEdgeInsetsMake(5, 5, 5, 5);
}

#pragma mark --UICollectionViewDelegate

-(BOOL)collectionView:(UICollectionView *)collectionView shouldSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    return YES;
}

- (UICollectionView *)collectionView {
    if (!_collectionView) {
        UICollectionViewFlowLayout  *flowLayout =[[UICollectionViewFlowLayout alloc]init];
        flowLayout.minimumLineSpacing = kCellVecteSpecing;
        flowLayout.minimumInteritemSpacing = kCellHorisenSpecing;
        flowLayout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
        _collectionView = [[UICollectionView alloc]initWithFrame:CGRectZero collectionViewLayout:flowLayout];
        _collectionView.backgroundColor = [UIColor whiteColor];
        _collectionView.delegate = self;
        _collectionView.dataSource = self;
        [self.collectionView registerClass:[AttentionCollectionViewCell class] forCellWithReuseIdentifier:CellIdentifier];
        [self.view addSubview:_collectionView];
    }
    return _collectionView;
}

- (UILabel *)label {
    if (!_label) {
        _label = [[UILabel alloc]init];
        _label.textAlignment = NSTextAlignmentCenter;
        _label.text = @"关注的户型";
        [self.view addSubview:_label];
    }
    return _label;
}

- (NSMutableArray *)models {
    if (!_models) {
        _models = [NSMutableArray array];
        [_models addObjectsFromArray:@[@"户型A",@"户型B",@"户型C",@"户型D"]];
    }
    return _models;
}

@end
